package com.gs.api.projects.bzapplets.web;

import com.github.pagehelper.PageInfo;
import com.gs.api.projects.bzapplets.entity.*;
import com.gs.api.projects.bzapplets.service.*;
import com.gs.api.config.Constant;
import com.gs.api.core.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author guoyunfei on 2021/9/23 21:58
 * @className DouyinWallpaperController
 * @description TODO 抖音壁纸API
 * @Version 1.0
 */
@Api(tags = "抖音壁纸信息")
@RestController
@RequestMapping("{appid}/platform")
public class BzAllWallpaperController {

    @Autowired
    private BzClassificationService bzClassificationService;

    @Autowired
    private BzBannerService bzBannerService;

    @Autowired
    private BzNoticeService bzNoticeService;

    @Autowired
    private BzConfigService bzConfigService;

    @Autowired
    private BzBaseInfoService bzBaseInfoService;

    @Autowired
    private BzAllWallpaperService bzAllWallpaperService;


    private final String ORDER_BY_ID = "id desc";
    private final String ORDER_BY_BROEWSE_COUNT = "browse_count desc";
    private final String ORDER_BY_LIKE_COUNT = "like_count desc";
    private final String ORDER_BY_COLLECTION_COUNT = "collection_count desc";

    /**
     * 设置排序
     * @param order
     * @return
     */
    public String getOrderBy(Integer order){
        if(null == order){
            return ORDER_BY_ID;
        }
        if(0 == order){
            return ORDER_BY_BROEWSE_COUNT;
        }else if(1 == order){
            return ORDER_BY_LIKE_COUNT;
        }else if(2 == order){
            return ORDER_BY_COLLECTION_COUNT;
        }
        return ORDER_BY_ID;
    }

    @ApiOperation(value = "壁纸接口列表", notes="壁纸接口列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码，默认1", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "条数，默认12", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "order", value = "排序字段0浏览数1点赞数2收藏数", required = false, dataType = "String"),
            @ApiImplicitParam(name = "openid", value = "用户微信openid", required = true, dataType = "String"),
            @ApiImplicitParam(name = "appid", value = "小程序appid", required = true, dataType = "String"),
    })
    @GetMapping("/wallpaper")
    private Result selectAllWallpaper(BzAllWallpaper wallpaper, Integer pageNum, Integer pageSize, Integer order, @PathVariable String appid){

        if(null == pageNum){
            pageNum = 1;
        }
        if(null == pageSize){
            pageSize = 12;
        }
        wallpaper.setAppid(appid);
        //排序
        String orderBy = getOrderBy(order);
        PageInfo<BzAllWallpaper> pageInfo = bzAllWallpaperService.selectAllWallpaper(wallpaper, pageNum, pageSize,orderBy);
        List<BzAllWallpaper> list = pageInfo.getList();
        pageInfo.setList(list);
        return Result.success(pageInfo);
    }


    /**
     * 查询图片详情
     * @param id 壁纸id
     * @return
     */
    @ApiOperation(value = "查看壁纸详情", notes="查看壁纸详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "壁纸ID", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "openid", value = "用户微信openid", required = true, dataType = "String"),
            @ApiImplicitParam(name = "appid", value = "小程序appid", required = true, dataType = "String"),
    })
    @GetMapping("/data.json")
    public Result get(Integer id,@PathVariable String appid){
        if(null == id){
            return Result.failure("获取参数失败");
        }

        Map<String,Object> map = new HashMap<>(6);
        BzAllWallpaper bzWallpaper = bzAllWallpaperService.getById(id);
        map.put("bzWallpaper",bzWallpaper);
        BzBaseInfo info = bzBaseInfoService.selectOne(appid);
        //工具栏
        map.put("switch_comment",info.getIsTools());
        //小程序名称
        map.put("title",info.getAppName());

        return Result.success(map);
    }

    @ApiOperation(value = "首页信息加载", notes="首页信息加载")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appid", value = "小程序appid", required = true, dataType = "String"),
    })
    @GetMapping("index")
    public Result index(@PathVariable String appid){
        Map<String,Object> map = new HashMap<>(6);
        // TODO 全局配置信息

        List<BzConfig> config = bzConfigService.getConfig(Constant.API_OSS_TYPE);
        String fileUrl = config.get(0).getValue();
        try {
            // TODO banner
            BzBanner banner = new BzBanner();
            banner.setEnable(1);
            banner.setAppid(appid);
            List<BzBanner> bzBannerList = bzBannerService.bannerList(banner);
            List<BzBanner> banners = new ArrayList<>();
            for (int i = 0; i < bzBannerList.size(); i++) {
                String imageUrl = fileUrl + bzBannerList.get(i).getImage();
                bzBannerList.get(i).setImage(imageUrl);
                if(bzBannerList.get(i).getType() == 2){

                }else{
                    banners.add(bzBannerList.get(i));
                }
            }
            map.put("banners",banners);
            // TODO banner end


            // TODO 分类
            BzClassification classification = new BzClassification();
            classification.setEnable(1);
            //多账号时开放
            classification.setAppid(appid);
            List<BzClassification> classifications = bzClassificationService.selectList(classification);
            map.put("classifications",classifications);

            // TODO 消息公告
            BzNotice notice = new BzNotice();
            notice.setEnable(1);
            List<BzNotice> notices = bzNoticeService.selectList(notice);
            map.put("notices",notices);
            BzBaseInfo info = bzBaseInfoService.selectOne(appid);
            //设置小程序logo
            map.put("logo",info.getAppLogo());
            map.put("title",info.getAppName());

            return Result.success(map);
        }catch (Exception e){
            return Result.failure("服务器错误");
        }
    }

}
